using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace RenderEngine
{
	internal class SkinnedModelRender : ModelRender
	{
		// TODO: Refactor: Animation progress and progress switching...
		SkinningData skinningData;
		AnimationPlayer animationPlayer;
		public SkinnedModelRender(Model model)
			: base(model)
		{
			skinningData = model.Tag as SkinningData;
			animationPlayer = new AnimationPlayer(skinningData);
			animationPlayer.StartClip(skinningData.AnimationClips["Run"]);
		}

		public override void Draw(GameTime gameTime, ref Matrix view, ref Matrix projection, ref Matrix world)
		{
			//animationPlayer.Update(gameTime.ElapsedGameTime, true, Matrix.CreateRotationX((float)Math.PI / 2) * world);
			animationPlayer.Update(gameTime.ElapsedGameTime, true, Matrix.CreateRotationX((float)Math.PI / 2) * world);
			Matrix[] transforms = new Matrix[this.Model.Bones.Count];
			Matrix[] bones = animationPlayer.GetSkinTransforms();
			this.Model.CopyAbsoluteBoneTransformsTo(transforms);

			foreach (ModelMesh mesh in this.Model.Meshes)
			{
				foreach (SkinnedEffect effect in mesh.Effects)
				{
					effect.SetBoneTransforms(bones);
					//effect.World = world;
					effect.View = view;
					effect.Projection = projection;
					effect.EnableDefaultLighting();
					effect.SpecularColor = Vector3.Zero;
				}
				mesh.Draw();
			}
		}
	}
}
